package com.wxq.yaoxia;


import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import com.wxq.net.NetClient;
import com.wxq.net.NetWorkThread;
import com.wxq.yaoxia.mainborad.YXMainboradActivity;
import com.wxq.yaoxia.util.YXConstants;
import com.wxq.yaoxia.util.YXUtility;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class YXLoginActivity extends YXBaseNetActivity {

	Button loginBtn;
	Button registerBtn;
	TextView registerTV;
	TextView forgetTV;
	Handler mHandler = new Handler();
	
	TextView tipTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentViewWithTopbar(R.layout.yx_login, R.layout.yx_topbar);
		
		tipTextView = (TextView) findViewById(R.id.hipTV);
		
		loginBtn = (Button) findViewById(R.id.loginBtn);
		registerBtn = (Button) findViewById(R.id.registerBtn);
		
		loginBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//获得网络数据
				NetWorkThread netWorkThread = new NetWorkThread(YXLoginActivity.this, YXLoginActivity.this, mHandler);
				netWorkThread.start();				
			}
		});
		
		registerBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent();
				intent.setClass(YXLoginActivity.this, YXRegisterActivity.class);
				YXLoginActivity.this.startActivity(intent);
			}
		});
		registerTV = (TextView) findViewById(R.id.registerTV);
		forgetTV = (TextView) findViewById(R.id.forgetPasswordTV);
		
		registerTV.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG );
		forgetTV.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG );
		
		registerTV.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent();
				intent.setClass(YXLoginActivity.this, YXRegisterActivity.class);
				YXLoginActivity.this.startActivity(intent);
			}
		});
		
		forgetTV.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 显示网页
				Intent intent = new Intent(YXLoginActivity.this, TestWebActivity.class);
				intent.putExtra("url", "http://www.yaoxia.com/forget01.jsp");
				YXLoginActivity.this.startActivity(intent);
				
			}
		});
		
		showTip();
	}

	@Override
	public void setTopBarButtonListener() {
		// TODO Auto-generated method stub
		Button leftBtn = (Button) findViewById(R.id.btnLeft);
    	Button rightBtnButton = (Button) findViewById(R.id.btnRight);
    	TextView centerTV = (TextView) findViewById(R.id.centerTV);
    	
    	leftBtn.setVisibility(View.VISIBLE);
    	leftBtn.setText("返回");
    	leftBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    	rightBtnButton.setVisibility(View.VISIBLE);
//    	rightBtnButton.setText("药");
    	rightBtnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(YXLoginActivity.this, YXMainboradActivity.class);
				YXLoginActivity.this.startActivity(intent);
			}
		});
    	
    	centerTV.setVisibility(View.VISIBLE);
    	centerTV.setText("请登录");
	}
	
	public void login() {
		
		String username =  ((EditText)findViewById(R.id.userNameET)).getText().toString();
		String password = ((EditText)findViewById(R.id.userPasswordET)).getText().toString();

		
		if(TextUtils.isEmpty(username)||
		   TextUtils.isEmpty(password))
		{			
			YXUtility.showToast(this, "请输入完整的登陆信息!", mHandler);
			return;
		}
		
		String timestamp = Calendar.getInstance().getTime().getTime()+""; 
		
		String checksum = YXUtility.encryptToMD5(username + timestamp + password + "yaoxia");
		String url = String.format(YXConstants.URL_LOGIN, username,password,timestamp,checksum.toLowerCase());
		//Log.d("wxq", "url = "+url);
		String result = NetClient.doGet(url);
		//Log.d("wxq", "result = "+result);
		if(TextUtils.isEmpty(result))
		{
			YXUtility.showToast(this, "Get data erro,please check your network!", mHandler);
			return ;
		}
		try {
			JSONObject resultJSON = new JSONObject(result);
			int code = resultJSON.getInt("code");
			String msg = resultJSON.getString("message");
			if(code == 500)
			{
				YXUtility.showToast(this, msg, mHandler);
				Log.d("wxq", msg);
			}
			else if(code == 200)
			{
				YXUtility.showToast(this, msg, mHandler);
				YXUser.currentUser.name = resultJSON.optJSONArray("userinfo").optJSONObject(0).optString("disname");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void getNetworkData() {
		login();	
	}

	@Override
	public void afterNetwork() {
		// TODO 自动生成的方法存根
		super.afterNetwork();
		showTip();
	}
	
	public void showTip() {
		String userName = YXUser.getCurrentUser().name;
		TextView centerTV = (TextView) findViewById(R.id.centerTV);
		if (TextUtils.isEmpty(userName)) {
			centerTV.setText("请登陆");
		}
		else {
			centerTV.setText(userName);
		}
	}

}
